<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="流程名称" prop="processName">
          <el-input
            v-model="queryParams.processName"
            placeholder="请输入流程名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="审批时间">
          <el-date-picker
            v-model="dateRange"
            style="width: 240px"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['00:00:00', '23:59:59']"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            v-hasPermi="['workflow:process:finishedExport']"
            @click="handleExport"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="finishedList" @selection-change="handleSelectionChange">
        <el-table-column label="序号" width="55" align="center">
          <template slot-scope="scope">
            <span>{{ (pageParams.current-1)*pageParams.size + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="申请人" align="center">
          <template slot-scope="scope">
            <label>{{scope.row.startUserNickName}}</label>
          </template>
        </el-table-column>
        <el-table-column label="流程名称" align="center" prop="procDefName"/>
        <el-table-column label="所属部门" align="center" prop="startDeptName"/>
        <el-table-column label="申请时间" align="center" prop="applyTime" width="180"/>
        <el-table-column label="审批时间" align="center" prop="finishTime" width="180"/>
        <el-table-column label="申请摘要" align="left" prop="summary" :show-overflow-tooltip="true" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-tickets"
              @click="handleFlowRecord(scope.row)"
              v-hasPermi="['workflow:process:startList']"
            >流转记录</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-tickets"
              @click="handleRevoke(scope.row)"
              v-hasPermi="['workflow:process:startList']"
            >撤回
            </el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="pageParams.current"
        :limit.sync="pageParams.size"
        @pagination="getList"
      />
    </div>
  </template>
  
  <script>
  import { listFinishedProcess, getProcessExternalForm } from '@/api/workflow/process';
  import { revokeProcess } from "@/api/workflow/finished";
  
  export default {
    name: "Finished",
    components: {
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 已办任务列表数据
        finishedList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        src: "",
        dateRange: [],
        pageParams: {
          current: 1,
          size: 10
        },
        // 查询参数
        queryParams: {
          name: null,
          category: null,
          key: null,
          tenantId: null,
          deployTime: null,
          derivedFrom: null,
          derivedFromRoot: null,
          parentDeploymentId: null,
          engineVersion: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    beforeRouteEnter(to, from, next) {
      next(vm => {
        vm.getList()
      })
    },
    methods: {
      /** 查询流程定义列表 */
      getList() {
        this.loading = true;
        listFinishedProcess(this.pageParams,this.queryParams).then(response => {
          this.finishedList = response.page.records;
          this.total = response.page.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          name: null,
          category: null,
          key: null,
          tenantId: null,
          deployTime: null,
          derivedFrom: null,
          derivedFromRoot: null,
          parentDeploymentId: null,
          engineVersion: null
        };
        this.resetForm("form");
      },
      setIcon(val){
        if (val){
          return "el-icon-check";
        }else {
          return "el-icon-time";
        }
  
      },
      setColor(val){
        if (val){
          return "#2bc418";
        }else {
          return "#b3bdbb";
        }
  
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加流程定义";
      },
      /** 流程流转记录 */
      handleFlowRecord(row){
        getProcessExternalForm({
          definitionId: row.procDefId,
          deployId: row.deployId
        }).then(res => {
          if (res.code==='200') {
            //vuex缓存参数
            let queryParams = {
              deployId: row.deployId,
              taskId: row.taskId,
              procInsId: row.procInsId,
              businessKey: row.businessKey,
              showApproval: false
            };
            window.localStorage.setItem("flowableDetailParams",JSON.stringify(queryParams));

            //跳转表单路由，显示表单填写页面
            const formUrl = res.data.formUrl;
            this.$router.push({
              path: `${formUrl}`
            })
          }
        })
      },
      /** 撤回任务 */
      handleRevoke(row) {
        const params = {
          procInsId: row.procInsId,
          taskId: row.taskId
        };
        revokeProcess(params).then(res => {
          this.$modal.msgSuccess(res.msg);
          this.getList();
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('workflow/process/finishedExport', {
          ...this.queryParams
        }, `wf_finished_process_${new Date().getTime()}.xlsx`)
      }
    }
  };
  </script>
  